﻿#region COPYRIGHT
//--------------------------------------------------------------------------------
// <copyright file="Gravity.cs" company="starLiGHT Entertainment Studios">
//    Copyright (c) 2008, 2009, 2010, 2011
//       Roland Rosenkranz (Glatzemann@email.de)
// </copyright>
// <license>
//   This file is part of starLiGHT.Particles.
//
//   starLiGHT.Particles is free software: you can redistribute it and/or modify
//   it under the terms of the GNU Lesser General Public License as published by
//   the Free Software Foundation, either version 3 of the License, or
//   (at your option) any later version.
//
//   starLiGHT.Particles is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU Lesser General Public License for more details.
//
//   You should have received a copy of the GNU Lesser General Public License
//   along with starLiGHT.Particles. If not, see http://www.gnu.org/licenses/.
//    
//   ADDITIONAL (commercial) LICENSES for starLiGHT.Particles are available on request.
// </license>
// <original_license>
//  * FLINT PARTICLE SYSTEM
//  * .....................
//  * 
//  * Author: Richard Lord
//  * Copyright (c) Richard Lord 2008-2009
//  * http://flintparticles.org
//  * 
//  * 
//  * Licence Agreement
//  * 
//  * Permission is hereby granted, free of charge, to any person obtaining a copy
//  * of this software and associated documentation files (the "Software"), to deal
//  * in the Software without restriction, including without limitation the rights
//  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  * copies of the Software, and to permit persons to whom the Software is
//  * furnished to do so, subject to the following conditions:
//  * 
//  * The above copyright notice and this permission notice shall be included in
//  * all copies or substantial portions of the Software.
//  * 
//  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  * THE SOFTWARE.
// </original_license>
// <version>
// **************[ starLiGHT.Engine SVN ]**********************
// * $Rev::                       $:  Revision of last commit *
// * $Author::                    $:  Author of last commit   *
// * $Date::                      $:  Date of last commit     *
// ************************************************************
// </version>
//--------------------------------------------------------------------------------
#endregion

namespace starLiGHT.ParticleSystem.Modifier.twoD
{
    #region Using Statements
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.Xna.Framework;
    using starLiGHT.ParticleSystem.Common;
    using starLiGHT.ParticleSystem.twoD;

    #endregion

    /// <summary>
    /// The Gravity Modifier applies a force to a particle to pull it towards a single point - 
    /// the center of the effect. The force applied is the inverse square of the distance from the 
    /// particle and the point.
    /// </summary>
    /// <remarks>
    /// <para>This is the same modifier as the AntiGravity modifier but with positive force.</para>
    /// <para>
    /// This is usually not usable for a world gravity that pulls all objects toward the ground. For this 
    /// gravity you should use the Acceleration Modifier.
    /// </para>
    /// </remarks>
    public class Gravity : Modifier2DBase
    {
#region Member Variables
        private float _x;
        private float _y;
        private float _power;
        private float _epsilonSq;
        private int _gravityConst = 10000;

        private Particle2D lp;
        private float lx;
        private float ly;
        private float ldSq;
        private float ld;
        private float lfactor;
#endregion

#region Constructors
        /// <summary>
        /// The constructor creates an AntiGravity modifier to be used by an emitter.
        /// </summary>
        public Gravity()
            : this(0, 0, 0, 100)
        {
        }

        /// <summary>
        /// The constructor creates a Gravity modifier to be used by an emitter.
        /// </summary>
        /// <param name="power">
        /// The power of the force - larger numbers produce a stronger force.
        /// </param>
        /// <param name="origin">
        /// The center of the Gravity modifier. This is from where all particles pushed away.
        /// </param>
        /// <param name="epsilon">
        /// The minimum distance for which the Gravity force is calculated. Particles closer than
        /// this distance experience the gravity as if they were this distance away. This stops the
        /// gravity effect blowing up as distances get small.
        /// <para>
        /// For realistic gravity effects you normaly use a small epsilon (~1.0f) but for a stable 
        /// visual effect a larger epsilon (~100.0f) is often the better solution.
        /// </para>
        /// </param>
        public Gravity(float power, Vector2 origin, float epsilon)
            : this(power, origin.X, origin.Y, epsilon)
        {
        }

        /// <summary>
        /// The constructor creates a Gravity modifier to be used by an emitter.
        /// </summary>
        /// <param name="power">
        /// The power of the force - larger numbers produce a stronger force.
        /// </param>
        /// <param name="x">
        /// The X coordinate of the center of the Gravity modifier. This is to where all particles are pulled.
        /// </param>
        /// <param name="y">
        /// The Y coordinate of the center of the Gravity modifier. This is to where all particles are pulled.
        /// </param>
        /// <param name="epsilon">
        /// The minimum distance for which the Gravity force is calculated. Particles closer than
        /// this distance experience the gravity as if they were this distance away. This stops the
        /// gravity effect blowing up as distances get small.
        /// <para>
        /// For realistic gravity effects you normaly use a small epsilon (~1.0f) but for a stable 
        /// visual effect a larger epsilon (~100.0f) is often the better solution.
        /// </para>
        /// </param>
        public Gravity(float power, float x, float y, float epsilon)
        {
            Power = power;
            _x = x;
            _y = y;
            Epsilon = epsilon;
        }

#endregion

        /// <summary>
        /// The power of the force - larger numbers produce a stronger force.
        /// </summary>
        public float Power
        {
            get { return _power / _gravityConst; }
            set { _power = value * _gravityConst; }
        }

        /// <summary>
        /// The X coordinate of the center of the Gravity modifier. This is to where all particles are pulled.
        /// </summary>
        public float X
        {
            get { return _x; }
            set { _x = value; }
        }

        /// <summary>
        /// The Y coordinate of the center of the Gravity modifier. This is to where all particles are pulled.
        /// </summary>
        public float Y
        {
            get { return _y; }
            set { _y = value; }
        }

        /// <summary>
        /// The minimum distance for which the Gravity force is calculated. Particles closer than
        /// this distance experience the gravity as if they were this distance away. This stops the
        /// gravity effect blowing up as distances get small.
        /// </summary>
        /// <remarks>
        /// For realistic gravity effects you normaly use a small epsilon (~1.0f) but for a stable 
        /// visual effect a larger epsilon (~100.0f) is often the better solution.
        /// </remarks>
        public float Epsilon
        {
            get { return (float)Math.Sqrt(this._epsilonSq); }
            set { this._epsilonSq = value * value; }
        }

        /// <summary>
        /// Calculate the gravity force on the particle and applies it for the specified time period.
        /// </summary>
        /// <remarks>
        /// This method is called by the emitter and need not be called by the user.
        /// </remarks>
        /// <param name="emitter">The Emitter that created the particle.</param>
        /// <param name="particle">The particle to update.</param>
        /// <param name="time">The time since the last update.</param>
        public override void Update(Emitter2D emitter, Particle2D particle, float time)
        {
            if (particle.mass == 0)
            {
                return;
            }

            lx = _x - particle.position.X;
            ly = _y - particle.position.Y;
            ldSq = lx * lx + ly * ly;
            if (ldSq == 0)
            {
                return;
            }

            ld = (float)Math.Sqrt(ldSq);
            if (ldSq < _epsilonSq)
            {
                ldSq = _epsilonSq;
            }

            lfactor = (_power * time) / (ldSq * ld);
            lp.velocity.X += lx * lfactor;
            lp.velocity.Y += ly * lfactor;
        }    
    }
}
